package com.jmz.apt.api.Helper;

import com.jmz.apt.api.Utils.JsonSerializerUtil;
import com.jmz.apt.api.dto.Dto;
import com.jmz.apt.api.entity.dbEntity.JmzAdminUser;
import com.jmz.apt.api.enums.ResponseCode;
import com.jmz.apt.api.message.response.Data;
import com.jmz.apt.api.message.response.Response;
import com.jmz.apt.api.message.response.ResponseMessage;
import com.jmz.apt.api.message.response.ResponseMessageTemplate;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by fanglexue on 2019/11/1.
 */

@Getter
@Component
public class ResponseHelper {

    private ResponseMessageTemplate responseMessageTemplate;


    public  ResponseHelper(){
        responseMessageTemplate = new ResponseMessageTemplate();
    }

    /**
     * Set response data
     * @param entities
     * @param <T>
     * @return
     */
    public <T extends Dto> void setResponseDataSet(String name,List<T> entities){
        com.jmz.apt.api.message.response.Data data = new com.jmz.apt.api.message.response.Data();
        data.setName(name);
        List<Object> objects = new ArrayList<>();
        for(T entity : entities){
            objects.add(entity);
        }
        data.setRow(objects);
        List<com.jmz.apt.api.message.response.Data> dataCollection = new ArrayList<>();
        dataCollection.add(data);
        responseMessageTemplate.setDataset(dataCollection);
    }

    public <T extends Dto> void setResponseMapSet(String name, Map map){
        Data data = new Data();
        data.setName(name);
        List<Object> objects = new ArrayList<>();
        objects.add(map);
        data.setRow(objects);
        List<Data> dataCollection = new ArrayList<>();
        dataCollection.add(data);
        responseMessageTemplate.setDataset(dataCollection);
    }

    public void setResponseQuery(Integer pageNo,Integer pageSize, Integer recountCount, Integer pageCount){
         responseMessageTemplate.setQueryResponse(pageNo,pageSize,recountCount,pageCount);
    }

    public void setResponseBCode(String code,String desc){
        responseMessageTemplate.setBussResponse(code,desc);
    }

    public ResponseMessageTemplate setResponseCode(String code,String desc){
        responseMessageTemplate.setBussResponse(code,desc);
        return  responseMessageTemplate;
    }

    public  void outPut(HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        PrintWriter out=null;
        try {
            out = response.getWriter() ;
            responseMessageTemplate.setDataset(new ArrayList<>());
            //responseMessageTemplate.setQueryResponse();
            responseMessageTemplate.initQueryResponse();
            ResponseMessage msg = responseMessageTemplate.getResponseMessage();
            out.write(JsonSerializerUtil.obj2String(msg));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            out.close();
        }
    }

}
